package com.qiumo.gmtool.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiumo.gmtool.common.RetResult;
import com.qiumo.gmtool.entity.UserAuthEntity;
import com.qiumo.gmtool.entity.auth.AuthVo;
import com.qiumo.gmtool.entity.vo.PermissionVo;
import com.qiumo.gmtool.service.UserAuthService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色授权
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-auth")
public class UserAuthController {

    private final UserAuthService userAuthService;

    private static final List<PermissionVo> PERMISSIONS_LIST = Arrays.asList(
        new PermissionVo("充值后台", "money"),
        new PermissionVo("物品后台", "item"),
        new PermissionVo("快捷功能", "fast"),
        new PermissionVo("角色功能", "avatar"),
        new PermissionVo("武器功能", "weapon")
    );

    /**
     * 获取所有的权限标识
     */
    @GetMapping("/list")
    public RetResult getList() {
        return RetResult.succ(PERMISSIONS_LIST);
    }

    /**
     * 获取用户的权限标识
     */
    @GetMapping("/listByUid/{uid}")
    @SaCheckRole("SUPERADMIN")
    public RetResult getList(@PathVariable String uid) {
        List<String> had = new ArrayList<>();
        List<UserAuthEntity> all = userAuthService.list(
            new LambdaQueryWrapper<UserAuthEntity>().eq(UserAuthEntity::getUid, uid)
        );
        if (all != null) {
            had = all.stream().map(UserAuthEntity::getAuthName).collect(Collectors.toList());
        }

        JSONObject json = new JSONObject();
        json.put("list", PERMISSIONS_LIST);
        json.put("had", had);
        return RetResult.succ(json);
    }

    /**
     * 为用户授权
     */
    @PostMapping("/auth")
    @SaCheckRole("SUPERADMIN")
    public RetResult auth(@RequestBody AuthVo authVo) {
        //删除原有授权
        userAuthService.remove(new LambdaQueryWrapper<UserAuthEntity>().eq(UserAuthEntity::getUid, authVo.getUid()));
        //判断新权限
        if (ObjectUtil.isNotEmpty(authVo.getList())) {
            List<String> list = authVo.getList();
            List<UserAuthEntity> collect = list.stream().map(vo -> {
                UserAuthEntity entity = new UserAuthEntity();
                entity.setUid(authVo.getUid());
                entity.setAuthName(vo);
                return entity;
            }).collect(Collectors.toList());
            userAuthService.saveBatch(collect);
        }
        return RetResult.succ("成功");
    }
}
